﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class ListFormNeedRepair : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindApplicationForm();
            }
        }

        /// <summary>
        ///朱经理(1049776210)  10:07:20
        ///这个上面工程师维修的地方显示2台 但是实际给工程师1台 
        ///朱经理(1049776210)  10:07:27
        ///另一台给另一个工程师了
        ///朱经理(1049776210)  10:07:29
        ///这个需要修改
        ///陈璞(624612156)  10:07:59
        ///现在就要改？
        ///朱经理(1049776210)  10:08:09
        ///恩
        ///朱经理(1049776210)  10:08:55
        ///意思就是 如果多台 例如 2台 一个给 A工程师 一个 给B工程师 那么 A能看到 的数量就1 不是2
        ///</summary>
        private void BindApplicationForm()
        {
            try
            {
                List<ApplicationForm> forms = new List<ApplicationForm>();
                ApplicationService service = new ApplicationService();
                if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.PlatformEngineer ||
                    ContextService.CurrentAccount.UserType.ID == (int)RoleType.Engineer)
                {
                    forms = service.GetProductNeedRepairByEngineer(ContextService.CurrentAccount.UserID, keyWordTextBox.Text.Trim());
                }
                else if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.PlatformAdmin)
                {
                    if (!ContextService.CurrentAccount.ServiceStationReference.IsLoaded)
                    {
                        ContextService.CurrentAccount.ServiceStationReference.Load();
                    }
                    forms = service.GetProductNeed2RepairOfPlatform(ContextService.CurrentAccount.ServiceStation.ID, keyWordTextBox.Text.Trim());
                }
                else
                {
                    forms = service.GetProductNeed2Repair(keyWordTextBox.Text.Trim());
                }

                applicationGridView.DataSource = forms;
                applicationGridView.DataBind();

                RecordsNumber = forms.Count;
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ListFormNeedRepair::BindApplicationForm");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void modifyLinkButton_click(object sender, EventArgs e)
        {
            string applicationid = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("FormNeedRepair.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationid));
        }

        protected void applicationGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < applicationGridView.PageCount)
            {
                applicationGridView.PageIndex = e.NewPageIndex;
            }
            
            BindApplicationForm();
        }

        protected void applicationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                    ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();

                    ApplicationForm applicationForm = e.Row.DataItem as ApplicationForm;
                    
                    ApplicationProductService productService = new ApplicationProductService();
                    List<ApplicationProduct> products = productService.GetApplicatonProductByFormID(applicationForm.ApplicationID);
                    if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.PlatformEngineer ||
                   ContextService.CurrentAccount.UserType.ID == (int)RoleType.Engineer)
                    {
                        products = products.Where(p=>p.User != null && p.User.UserID == ContextService.CurrentAccount.UserID).ToList();
                    }
                    else if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.PlatformAdmin)
                    {
                        if (!ContextService.CurrentAccount.ServiceStationReference.IsLoaded)
                        {
                            ContextService.CurrentAccount.ServiceStationReference.Load();
                        }
                        products = products.Where(p => p.User != null && p.User.ServiceStation.ID == ContextService.CurrentAccount.ServiceStation.ID).ToList();
                    }
                    string repariStatus = "维修状态： ";
                    string quoteSattus = "报价状态: ";
                    string RelProductTypes = "";
                    foreach (ApplicationProduct applicationProduct in products)
                    {
                        if (applicationProduct.DistributeDateTime.HasValue)
                        {
                            List<string> statusList = productService.GetApplicationProductRepairStatus(applicationProduct);
                            repariStatus += statusList[0] + "/";
                            quoteSattus += statusList[2] + "/";

                            if (!applicationProduct.ProductTypeReference.IsLoaded)
                            {
                                applicationProduct.ProductTypeReference.Load();
                            }
                            string productType = applicationProduct.ProductType.ProductTypeName + ";";
                            if (!RelProductTypes.Contains(productType))
                            {
                                RelProductTypes += productType;
                            }
                        }
                    }
                    repariStatus += "; " + quoteSattus;
                    Label statusLbl = e.Row.FindControl("statusLbl") as Label;
                    Label Label_ProductType = e.Row.FindControl("Label_ProductType") as Label;
                    statusLbl.ToolTip = repariStatus;
                    Label_ProductType.Text = RelProductTypes;
                }
                if (e.Row.RowType == DataControlRowType.Pager)
                {
                    DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                    for (int index = 1; index <= applicationGridView.PageCount; index++)
                    {
                        pageDDL.Items.Add(index.ToString());
                    }
                    pageDDL.SelectedValue = (applicationGridView.PageIndex + 1).ToString();
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ListFormNeedRepair::applicationGridView_RowDataBound");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<ApplicationForm> dds = new List<ApplicationForm>();
            dds.Add(new ApplicationForm());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            applicationGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindApplicationForm();
        }

        protected void applicationGridView_DataBound(object sender, EventArgs e)
        {
            if (applicationGridView.BottomPagerRow != null)
            {
                applicationGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindApplicationForm();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindApplicationForm();
        }
    }
}
